package
{
	import flash.display.MovieClip;

	public class ParticleManager
	{
		public var buffer_f;
		public var buffer_b;
		public var curDepthF:Number;
		public var curDepthB:Number;
		public var maxDepth:Number;
		public var counterF:Number;
		public var counterB:Number;
		public var effectList:Object;

		public function ParticleManager(buffer_f, buffer_b)
		{
			this.buffer_f = buffer_f;
			this.buffer_b = buffer_b;
			this.curDepthF = 0;
			this.curDepthB = 0;
			this.maxDepth = 100;
			this.counterF = 0;
			this.counterB = 0;
			this.effectList = new Object();
			var _local2 = new Array();
			_local2.push("debugDustMC1");
			_local2.push("debugDustMC2");
			this.effectList[ParticleManager.FXTYPE_SKIDDUST] = new ParticleEffect(_local2, 7, 3);
			this.effectList[ParticleManager.FXTYPE_JUMPDUST] = new ParticleEffect(_local2, 0, 0);
			this.effectList[ParticleManager.FXTYPE_RAGDUST] = new ParticleEffect(_local2, 10, 2);
			var temp2 = new Array();
			temp2.push("debugBloodSpurtMC1");
			temp2.push("debugBloodSpurtMC2");
			this.effectList[ParticleManager.FXTYPE_BLOODSPURT] = new ParticleEffect(temp2, 0, 0);
			var temp3 = new Array();
			temp3.push("debugChainFlashMC1");
			temp3.push("debugChainFlashMC2");
			this.effectList[ParticleManager.FXTYPE_CHAINFLASH] = new ParticleEffect(temp3, 0, 0);
			var temp4 = new Array();
			temp4.push("debugChainDebrisMC1");
			temp4.push("debugChainDebrisMC2");
			temp4.push("debugChainDebrisMC3");
			this.effectList[ParticleManager.FXTYPE_CHAINDEBRIS] = new ParticleEffect(temp4, 0, 0);
			var temp5 = new Array();
			temp5.push("debugChainBulletMC1");
			this.effectList[ParticleManager.FXTYPE_CHAINBULLET] = new ParticleEffect(temp5, 0, 0);
			var temp6 = new Array();
			temp6.push("debugLaserSparkMC1");
			temp6.push("debugLaserSparkMC2");
			temp6.push("debugLaserSparkMC3");
			this.effectList[ParticleManager.FXTYPE_LASERSPARK] = new ParticleEffect(temp6, 6, 4);
			var temp7 = new Array();
			temp7.push("debugLaserChargeMC1");
			temp7.push("debugLaserChargeMC2");
			temp7.push("debugLaserChargeMC3");
			this.effectList[ParticleManager.FXTYPE_LASERCHARGE] = new ParticleEffect(temp7, 2, 3);
			var temp8 = new Array();
			temp8.push("debugZapMC1");
			temp8.push("debugZapMC2");
			temp8.push("debugZapMC3");
			this.effectList[ParticleManager.FXTYPE_ZAP] = new ParticleEffect(temp8, 0, 0);
			var _local3 = new Array();
			_local3.push("debugZapVMC1");
			_local3.push("debugZapVMC2");
			_local3.push("debugZapVMC3");
			this.effectList[ParticleManager.FXTYPE_ZAPV] = new ParticleEffect(_local3, 0, 0);
			var temp10 = new Array();
			temp10.push("debugTurretBulletMC1");
			this.effectList[ParticleManager.FXTYPE_TURRETBULLET] = new ParticleEffect(temp10, 0, 0);
			var temp11 = new Array();
			temp11.push("debugTurretDebrisMC1");
			this.effectList[ParticleManager.FXTYPE_TURRETDEBRIS] = new ParticleEffect(temp11, 0, 0);
			var temp12 = new Array();
			temp12.push("debugFireBallMC1");
			temp12.push("debugFireBallMC2");
			temp12.push("debugFireBallMC3");
			this.effectList[ParticleManager.FXTYPE_FIREBALL] = new ParticleEffect(temp12, 0, 0);
			var temp13 = new Array();
			temp13.push("debugFireBurstMC1");
			temp13.push("debugFireBurstMC2");
			this.effectList[ParticleManager.FXTYPE_FIREBURST] = new ParticleEffect(temp13, 0, 0);
			var temp14 = new Array();
			temp14.push("debugRocketSmokeMC1");
			temp14.push("debugRocketSmokeMC2");
			temp14.push("debugRocketSmokeMC3");
			this.effectList[ParticleManager.FXTYPE_ROCKETSMOKE] = new ParticleEffect(temp14, 3, 2);
		}

		public static const FXTYPE_SKIDDUST:Number = 0;
		public static const FXTYPE_JUMPDUST:Number = 1;
		public static const FXTYPE_BLOODSPURT:Number = 2;
		public static const FXTYPE_RAGDUST:Number = 3;
		public static const FXTYPE_CHAINBULLET:Number = 4;
		public static const FXTYPE_CHAINDEBRIS:Number = 5;
		public static const FXTYPE_CHAINFLASH:Number = 6;
		public static const FXTYPE_LASERSPARK:Number = 7;
		public static const FXTYPE_LASERCHARGE:Number = 8;
		public static const FXTYPE_ZAP:Number = 9;
		public static const FXTYPE_ZAPV:Number = 10;
		public static const FXTYPE_TURRETBULLET:Number = 11;
		public static const FXTYPE_TURRETDEBRIS:Number = 12;
		public static const FXTYPE_FIREBURST:Number = 13;
		public static const FXTYPE_FIREBALL:Number = 14;
		public static const FXTYPE_ROCKETSMOKE:Number = 15;

		public function SpawnParticle_Rand(FXTYPE):MovieClip
		{
			var _local2 = this.effectList[FXTYPE];
			_local2.counter = _local2.counter - ((this.counter++) % _local2.rand);
			if (_local2.counter < 0)
			{
				var _local3 = this.buffer_f.attachMovie(_local2.mcList[this.curDepthF % _local2.mcNum], "pfx" + this.curDepthF, this.curDepthF);
				_local2.counter = _local2.rate;
				if (this.maxDepth < (this.curDepthF++))
				{
					this.curDepthF = 0;
					this.counterF = 0;
				}
				return(_local3);
			}
			return null;
			return(_local3);
		}

		public function SpawnParticle_Int(FXTYPE):MovieClip
		{
			var _local2 = this.effectList[FXTYPE];
			_local2.counter = _local2.counter - 1;
			if (_local2.counter < 0)
			{
				var _local3 = this.buffer_f.attachMovie(_local2.mcList[this.curDepthF % _local2.mcNum], "pfx" + this.curDepthF, this.curDepthF);
				_local2.counter = _local2.rate;
				if (this.maxDepth < (this.curDepthF++))
				{
					this.curDepthF = 0;
					this.counterF = 0;
				}
				return(_local3);
			}
			return null;
			return(_local3);
		}

		public function SpawnParticle(FXTYPE):MovieClip
		{
			var _local2 = this.effectList[FXTYPE];
			var _local3 = this.buffer_f.attachMovie(_local2.mcList[this.curDepthF % _local2.mcNum], "pfx" + this.curDepthF, this.curDepthF);
			if (this.maxDepth < (this.curDepthF++))
			{
				this.curDepthF = 0;
				this.counterF = 0;
			}
			return(_local3);
			return(_local3);
		}

		public function SpawnParticleB(FXTYPE):MovieClip
		{
			var _local2 = this.effectList[FXTYPE];
			var _local3 = this.buffer_b.attachMovie(_local2.mcList[this.curDepthB % _local2.mcNum], "pfx" + this.curDepthB, this.curDepthB);
			if (this.maxDepth < (this.curDepthB++))
			{
				this.curDepthB = 0;
				this.counterB = 0;
			}
			return(_local3);
			return(_local3);
		}

		public function SpawnFloorDust(pos, rad, norm, rot, dir, strength):void
		{
			var _local1 = this.SpawnParticle_Rand(ParticleManager.FXTYPE_SKIDDUST);
			if (_local1 != 0)
			{
				_local1.x = pos.x - (norm.x * rad);
				_local1.y = pos.y - (norm.y * rad);
				_local1.rotation = (rot - (dir * 8)) + ((Math.random() * 10) - 5);
				_local1.scaleX = dir * (10 + (strength * 10));
				_local1.scaleY = 10;
			}
		}

		public function SpawnWallDust(pos, rad, norm, strength):void
		{
			var _local2 = rad;
			var _local3 = norm;
			var _local1 = this.SpawnParticle_Rand(ParticleManager.FXTYPE_SKIDDUST);
			if (_local1 != 0)
			{
				_local1.x = pos.x - (_local3.x * _local2);
				_local1.y = (pos.y - (_local3.y * _local2)) - (((Math.random() * _local2) * 2) - _local2);
				_local1.rotation = (90 - (_local3.x * 8)) + ((Math.random() * 10) - 5);
				_local1.scaleX = 10 + (strength * 20);
				_local1.scaleY = 10;
			}
		}

		public function SpawnJumpDust(px, py, rot):void
		{
			var _local2 = 1;
			var _local3 = 4;
			while (_local3--)
			{
				var _local1 = this.SpawnParticle(ParticleManager.FXTYPE_JUMPDUST);
				_local1.x = px;
				_local1.y = py;
				_local1.rotation = (rot - (_local2 * 20)) + ((Math.random() * 20) - 10);
				_local1.scaleX = _local2 * (10 + (Math.random() * 8));
				_local1.scaleY = 10 + (Math.random() * 5);
				_local2 = _local2 * -1;
			}
		}

		public function SpawnLandDust(px, py, rot, strength):void
		{
			var _local3 = strength;
			var _local2 = 1;
			var n = 4;
			while (n--)
			{
				var _local1 = this.SpawnParticle(ParticleManager.FXTYPE_JUMPDUST);
				_local1.x = px;
				_local1.y = py;
				_local1.rotation = (rot - (_local2 * 40)) + ((Math.random() * 20) - 10);
				_local1.scaleX = _local2 * ((5 + (Math.random() * 5)) + _local3);
				_local1.scaleY = 15 + (_local3 * 2);
				_local2 = _local2 * -1;
			}
		}

		public function SpawnBloodSpurt(px, py, vx, vy, n):void
		{
			var _local3 = vy;
			while (n--)
			{
				var _local2 = this.SpawnParticle(ParticleManager.FXTYPE_BLOODSPURT);
				var _local1 = Math.random;
				_local2.x = px - ((_local1() * 8) - 4);
				_local2.y = py - ((_local1() * 8) - 4);
				_local2.scaleX = (vx * (6 + (_local1() * 3))) - ((_local1() * 60) - 30);
				_local2.scaleY = (_local3 * (6 + (_local1() * 3))) - ((_local1() * 60) - 30);
			}
		}

		public function SpawnRagBloodSpurt(px, py, vx, vy):void
		{
			var _local2 = this.SpawnParticle(ParticleManager.FXTYPE_BLOODSPURT);
			var _local1 = Math.random;
			_local2.x = px - ((_local1() * 8) - 4);
			_local2.y = py - ((_local1() * 8) - 4);
			_local2.scaleX = (vx * (6 + (_local1() * 3))) - ((_local1() * 40) - 20);
			_local2.scaleY = (vy * (6 + (_local1() * 3))) - ((_local1() * 40) - 20);
		}

		public function SpawnRagDust(pos, rad, nx, ny, strength):void
		{
			var _local2 = ny;
			var _local3 = nx;
			var _local1 = this.SpawnParticle_Rand(ParticleManager.FXTYPE_RAGDUST);
			if (_local1 != 0)
			{
				_local3 = _local3 / strength;
				_local2 = _local2 / strength;
				_local1.x = pos.x - (_local3 * rad);
				_local1.y = pos.y - (_local2 * rad);
				_local1.rotation = Main.getInstance().NormToRot(_local3, _local2) + ((Math.random() * 20) - 10);
				_local1.scaleX = 20 + (2 * strength);
				_local1.scaleY = 10;
			}
		}

		public function SpawnRocketSmoke(pos, rot):void
		{
			var _local1 = this.SpawnParticle_Rand(ParticleManager.FXTYPE_ROCKETSMOKE);
			if (_local1 != 0)
			{
				_local1.x = pos.x;
				_local1.y = pos.y;
				_local1.rotation = rot + (10 * ((Math.random() * 2) - 1));
				_local1.scaleX = 20 + (Math.random() * 20);
				_local1.scaleY = 20 + (Math.random() * 20);
			}
		}

		public function SpawnRocketDeath(pos, rot):void
		{
			var mc1 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			var mc2 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			var _local3 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			var _local2 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			mc1.x = (mc2.x = (_local3.x = (_local2.x = pos.x)));
			mc1.y = (mc2.y = (_local3.y = (_local2.y = pos.y)));
			var _local1 = Math.random;
			var r1 = _local1();
			var r2 = _local1();
			var r3 = _local1();
			var r4 = _local1();
			var r5 = _local1();
			mc1.scaleX = (mc2.scaleX = 20 + (r3 * 20));
			_local3.scaleX = (_local2.scaleX = 20 + (r4 * 30));
			mc1.scaleY = (_local2.scaleY = 20 + (r5 * 20));
			mc2.scaleY = (_local3.scaleY = 20 + (r1 * 10));
			mc1.rotation = rot + (r1 * 20);
			mc2.rotation = rot - (r2 * 30);
			_local3.rotation = rot + (r5 * 40);
			_local2.rotation = rot - (r3 * 40);
		}

		public function SpawnExplosion(pos):void
		{
			var mc = this.SpawnParticle(ParticleManager.FXTYPE_FIREBURST);
			var mc1 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			var mc2 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			var _local3 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			var _local2 = this.SpawnParticle(ParticleManager.FXTYPE_FIREBALL);
			mc.x = (mc1.x = (mc2.x = (_local3.x = (_local2.x = pos.x))));
			mc.y = (mc1.y = (mc2.y = (_local3.y = (_local2.y = pos.y))));
			var _local1 = Math.random;
			var r1 = _local1();
			var r2 = _local1();
			var r3 = _local1();
			var r4 = _local1();
			var r5 = _local1();
			mc.scaleX = 15 + (r1 * 15);
			mc.scaleY = 15 + (r2 * 15);
			mc1.scaleX = (mc2.scaleX = 20 + (r3 * 20));
			_local3.scaleX = (_local2.scaleX = 20 + (r4 * 30));
			mc1.scaleY = (_local2.scaleY = 20 + (r5 * 20));
			mc2.scaleY = (_local3.scaleY = 20 + (r1 * 10));
			mc1.rotation = 360 * r1;
			mc2.rotation = 360 * r2;
			_local3.rotation = 360 * r5;
			_local2.rotation = 360 * r3;
		}

		public function SpawnTurretBullet(a, b, rot):void
		{
			var _local3 = this.SpawnParticle(ParticleManager.FXTYPE_TURRETBULLET);
			_local3.x = a.x;
			_local3.y = a.y;
			_local3.scaleX = b.x - a.x;
			_local3.scaleY = b.y - a.y;
			var _local2 = this.SpawnParticle(ParticleManager.FXTYPE_TURRETDEBRIS);
			var _local1 = this.SpawnParticle(ParticleManager.FXTYPE_TURRETDEBRIS);
			_local2.x = (_local1.x = b.x);
			_local2.y = (_local1.y = b.y);
			var rnd = Math.random;
			_local2.scaleX = (_local1.scaleY = 40 + (rnd() * 20));
			_local1.scaleX = (_local2.scaleY = 20 + (rnd() * 40));
			_local2.rotation = rot + (5 + (rnd() * 15));
			_local1.rotation = rot - (5 + (rnd() * 15));
		}

		public function SpawnLaserSpark(pos, dx, dy):void
		{
			// var _local1 = this.SpawnParticleB_Int(ParticleManager.FXTYPE_LASERCHARGE);
			var _local1 = this.SpawnParticle_Int(ParticleManager.FXTYPE_LASERCHARGE);
			if (_local1 != 0)
			{
				_local1.x = pos.x;
				_local1.y = pos.y;
				_local1.scaleX = (-dx) * (30 + (40 * ((Math.random() * 2) - 1)));
				_local1.scaleY = (-dy) * (30 + (40 * ((Math.random() * 2) - 1)));
			}
		}

		public function SpawnLaserCharge(pos):void
		{
			var _local1 = this.SpawnParticle_Rand(ParticleManager.FXTYPE_LASERCHARGE);
			if (_local1 != 0)
			{
				_local1.x = pos.x;
				_local1.y = pos.y;
				_local1.scaleX = 20 + (Math.random() * 20);
				_local1.scaleY = 10 + (Math.random() * 20);
				_local1.rotation = Math.random() * 360;
			}
		}

		public function SpawnZap(px, py, rot):void
		{
			var _local2 = Math.random;
			var _local3 = 6;
			while (_local3--)
			{
				var _local1 = this.SpawnParticle(ParticleManager.FXTYPE_ZAP);
				_local1.x = px;
				_local1.y = py;
				_local1.scaleX = 30 + (_local2() * 30);
				_local1.scaleY = 30 + (_local2() * 20);
				_local1.rotation = rot + (20 * ((_local2() * 2) - 1));
			}
		}

		public function SpawnZapThwompH(pos, xw, yw, targ):void
		{
			var _local3 = xw;
			var _local2 = Math.random;
			var n = 6;
			while (n--)
			{
				var _local1 = this.SpawnParticle(ParticleManager.FXTYPE_ZAP);
				_local1.x = pos.x + _local3;
				_local1.y = (pos.y - yw) + (yw * _local2());
				_local1.scaleX = (4 * _local3) + (20 * ((_local2() * 2) - 1));
				_local1.scaleY = 60 + (60 * _local2());
			}
		}

		public function SpawnZapThwompV(pos, xw, yw, targ):void
		{
			var _local3 = xw;
			var _local2 = Math.random;
			var n = 6;
			while (n--)
			{
				var _local1 = this.SpawnParticle(ParticleManager.FXTYPE_ZAPV);
				_local1.y = pos.y + yw;
				_local1.x = (pos.x - _local3) + (_local3 * _local2());
				_local1.scaleY = (4 * yw) + (20 * ((_local2() * 2) - 1));
				_local1.scaleX = 60 + (60 * _local2());
			}
		}

		public function SpawnChainBullet(a, b, len, rot):void
		{
			var r1 = ((Math.random() * 2) - 1);
			var r2 = ((Math.random() * 2) - 1);
			var r3 = ((Math.random() * 2) - 1);
			var _local1 = this.SpawnParticle(ParticleManager.FXTYPE_CHAINFLASH);
			var mcb = this.SpawnParticle(ParticleManager.FXTYPE_CHAINBULLET);
			mcb.scaleX = len;
			_local1.x = (mcb.x = a.x);
			_local1.y = (mcb.y = a.y);
			_local1.scaleX = 30 + (r1 * 10);
			_local1.scaleY = 20 + (r2 * 20);
			_local1.rotation = (mcb.rotation = rot);
			var _local3 = this.SpawnParticle(ParticleManager.FXTYPE_CHAINDEBRIS);
			var _local2 = this.SpawnParticle(ParticleManager.FXTYPE_CHAINDEBRIS);
			_local3.x = (_local2.x = b.x);
			_local3.y = (_local2.y = b.y);
			_local3.scaleX = 30 + (15 * r2);
			_local2.scaleX = 30 + (15 * r3);
			rot = rot - 180;
			_local3.rotation = rot + (15 * r1);
			_local2.rotation = rot + (15 * r2);
		}

		public function SpawnParticle_Debug(PTYPE, x, y, rot, dir, scalex, scaley):void
		{
		}
	}
}